package com.zx.oa.controller;

import com.zx.oa.entity.Student;
import com.zx.oa.service.student.StudentService;
import com.zx.oa.service.student.StudentServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/modifyServlet.do")
public class StudentModifyServlet extends HttpServlet {

    StudentService studentService=new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String caozuo=request.getParameter("param");
        if("cha".equals(caozuo)){
            chaServlet(request,response);
        } else if ("modify".equals(caozuo)) {
            modifyServlet(request,response);
        }
    }

    private void modifyServlet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Integer id= Integer.valueOf(request.getParameter("id"));
        String name=request.getParameter("name");
        Integer classId= Integer.valueOf(request.getParameter("classID"));
        String fromSchool=request.getParameter("fromschool");
        String education=request.getParameter("education");
        String loginCode=request.getParameter("username");
        String password=request.getParameter("userpwd");
        Student student=new Student(id,name,classId,fromSchool,education,loginCode,password);

        boolean flag=studentService.modifyStudent(student);
        if(flag){
            String namec=request.getParameter("namec");
            String classname=request.getParameter("classname");
            String major1=request.getParameter("major1");
            Integer index= Integer.valueOf(request.getParameter("pageIndex"));
            request.setAttribute("namec",namec);
            request.setAttribute("classname",classname);
//            request.setAttribute("major",major1);
            request.setAttribute("pageIndex",index);
            request.getRequestDispatcher("chaservlet.do?major="+major1).forward(request,response);
//            response.sendRedirect("chaservlet.do");
        }
    }

    private void chaServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id= Integer.valueOf(request.getParameter("stuNo"));
        Student student=studentService.getStudentById(id);

        String major=request.getParameter("major");
        String className=request.getParameter("className");
        request.setAttribute("studentInfo",student);
        request.setAttribute("majorInfo",major);
        request.setAttribute("className",className);

        String name=request.getParameter("namec");
        String classname=request.getParameter("classname");
        String major1=request.getParameter("major1");
        Integer index= Integer.valueOf(request.getParameter("pageIndex"));
        request.setAttribute("namec",name);
        request.setAttribute("classname",classname);
        request.setAttribute("major1",major1);
        request.setAttribute("pageIndex",index);
        request.getRequestDispatcher("jsp/studentModify.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
